package com.chenxin.wms.service.impl;

import java.util.Date;
import java.util.List;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.chenxin.common.constant.WmsConstants;
import com.chenxin.common.core.domain.ProcResult;
import com.chenxin.common.enums.WmsBusinessStatus;
import com.chenxin.common.exception.ServiceException;
import com.chenxin.common.exception.wms.WmsException;
import com.chenxin.wms.domain.ModuleSerial;
import com.chenxin.wms.domain.ObDetailEntity;
import com.chenxin.wms.domain.vo.ObMasterVO;
import com.chenxin.wms.mapper.ObDetailMapper;
import com.chenxin.wms.process.outbound.IOutboundProcess;
import com.chenxin.wms.process.outbound.IProcObMasterExecutor;
import com.chenxin.wms.process.outbound.IProcPrepareCancelByMaster;
import com.chenxin.wms.process.outbound.IProcStockPrepareByMaster;
import com.chenxin.wms.service.IModuleSerialService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Map;
import java.util.stream.Collectors;

import com.chenxin.common.utils.StringUtils;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import com.chenxin.wms.mapper.ObMasterMapper;
import com.chenxin.wms.domain.ObMasterEntity;
import com.chenxin.wms.service.IObMasterService;
import org.springframework.util.CollectionUtils;

/**
 * 生产出库单Service业务层处理
 *
 * @author gry
 * @date 2025-04-13
 */
@Service
public class ObMasterServiceImpl implements IObMasterService {
    @Autowired
    private ObMasterMapper obMasterMapper;
    @Autowired
    private IProcStockPrepareByMaster procStockPrepareByMaster;
    @Autowired
    private IProcPrepareCancelByMaster procPrepareCancelByMaster;
    @Autowired
    private IProcObMasterExecutor procObMasterExecutor;

    /**
     * 查询生产出库单列表
     *
     * @param obMasterVo 生产出库单
     * @return 生产出库单
     */
    @Override
    public List<ObMasterVO> selectObMasterList(ObMasterVO obMasterVo) {
        return obMasterMapper.selectObMasterViewList(obMasterVo);
    }

    /**
     * 新增生产出库单
     *
     * @param obMasterEntity 生产出库单
     * @return 结果
     */
    @Override
    public int insertObMaster(ObMasterEntity obMasterEntity) {
        return obMasterMapper.insert(obMasterEntity);
    }

    /**
     * 修改生产出库单
     *
     * @param obMasterEntity 生产出库单
     * @return 结果
     */
    @Override
    public int updateObMaster(ObMasterEntity obMasterEntity) {
        return obMasterMapper.updateById(obMasterEntity);
    }

    /**
     * 批量删除生产出库单
     *
     * @param obMasterIds 需要删除的生产出库单主键
     * @return 结果
     */
    @Override
    public int deleteObMasterByObMasterIds(Long[] obMasterIds, Long updater, String updateName) {
        return 0;
    }

    @Override
    public ProcResult operateObMasterByIds(List<Long> obMasterIds, String operateType, Long creator) {
        // 定义局部变量
        ProcResult result = ProcResult.success("相关单据备货成功");
        // 判断obMasterIds是否为空,以及类型是否可用(备货、取消、执行等)
        if (obMasterIds == null || obMasterIds.size() == 0) {
            result = ProcResult.error("obMasterIds_null", "待操作的主单号不能为空!操作类型【" + operateType + "】");
        }

        int successCount = 0;
        int failCount = 0;
        // 循环处理obMasterIds,并根据类型调用不同的作业方法
        if (WmsConstants.STOCK_PREPARE.equals(operateType)) {
            for (Long obMasterId : obMasterIds) {
                try {
                    // 调用主单备货方法
                    result = procStockPrepareByMaster.call(obMasterId, creator);
                    if (WmsConstants.PROC_SUCCESS.equals(result.getCode())) {
                        // 累计成功备货单据
                        successCount++;
                    } else {
                        // 累计失败备货单据
                        failCount++;
                    }
                } catch (WmsException e) {
                    // 累计异常备货单据
                    failCount++;
                }
            }
            result.setMessage("备货完成!成功【" + successCount + "】条，失败【" + failCount + "】条");
        } else if (WmsConstants.PREPARE_CANCEL.equals(operateType)) {
            // 调用备货取消方法
            for (Long obMasterId : obMasterIds) {
                try {
                    result = procPrepareCancelByMaster.call(obMasterId, creator);
                    if (WmsConstants.PROC_SUCCESS.equals(result.getCode())) {
                        successCount++;
                    } else {
                        failCount++;
                    }
                } catch (WmsException e) {
                    failCount++;
                }
            }

            result.setMessage("备货取消完成!成功取消【" + successCount + "】条，失败【" + failCount + "】条");

        } else if (WmsConstants.OUT_EXEC.equals(operateType)) {
            // 调用出库执行方法
            for (Long obMasterId : obMasterIds) {
                try {
                    result = procObMasterExecutor.call(obMasterId, creator);
                    if (WmsConstants.PROC_SUCCESS.equals(result.getCode())) {
                        successCount++;
                    } else {
                        failCount++;
                    }
                } catch (WmsException e) {
                    failCount++;
                }
            }
            result.setMessage("出库执行完成!成功执行【" + successCount + "】条，失败【" + failCount + "】条");
        } else {
            throw new ServiceException("操作类型【" + operateType + "】无法处理!请核查");
        }

        return result;
    }
}
